//
//  FileDownloadMessage.swift
//  Trans
//
//  Created by kologe on 2024/10/19.
//

import Foundation
import Network

let DOWNLOADFAILED = INT32(-1)
let DOWNLOADING = INT32(0)
let DOWNLOADCOMPLETE = INT32(1)
let DOWNLOADCANCEL = INT32(2)
let DOWNLOADPAUSE = INT32(3)

//文件下载信息
class FileTransMessage: Equatable,Identifiable,Codable{
    var absolutePath:String?
    var fileName:String? //文件下载位置
    var totalSize:FSIZE? //总的byte
    var downloadSize:FSIZE = 0 // 显示已经下载的byte
    
    var startTime:TimeInterval //开始数据的传输的时间
    
    var state:INT32// 显示是否还在下载中
    var ip:String
    
    //是否传输，如果是listener就是接受，如果是client就是发送
    var ifTrans :UINT32 = 0
    var conID:ConId = 0
    var fileId:FileId = 0
    
//    private var _connection:ConnectionWrapper?
//    
//    weak var connection:ConnectionWrapper?{
//        get{
//            return _connection
//        }
//        set{
//            _connection = newValue
//            updateIP()
//        }
//   
//    }
//    
//    func updateIP(){
//        if _connection != nil{
//            let str = _connection?.connection.endpoint.debugDescription
//            self.ip = (str?.components(separatedBy: ":")[0])!
//        }
//    }
    init(){
        self.fileName = "default"
        totalSize = 0
        downloadSize = 0
        state = INT32(DOWNLOADING)
        startTime = Date().timeIntervalSince1970
        ip = ""
    }

    
    init(fileName: String? = nil, totalSize: UInt64? = nil, downloadSize: UInt64? = nil, state: INT32 = INT32(DOWNLOADING), absolutePath:String?=nil,startTime:TimeInterval = Date().timeIntervalSince1970 ) {
        self.absolutePath = absolutePath
        self.fileName = fileName
        self.totalSize = totalSize
        self.downloadSize = downloadSize!
        self.state = state
        self.startTime = startTime
        ip = ""
    }
    
    
    static func == (lhs: FileTransMessage, rhs: FileTransMessage) -> Bool {
        return lhs.fileName == rhs.fileName && lhs.totalSize == rhs.totalSize &&  lhs.downloadSize == rhs.downloadSize && lhs.state == rhs.state
    }
    
    func LogMsg() -> String{
        let endPointStr = kzListener.conDic[self.conID]?.con.endpoint.debugDescription
        let logstr =  String(format: "fileName:%@\nabsolutePath:%@\ndownloadSize:%ld\ntotalSize:%ld\nip:%@\n", self.fileName!,self.absolutePath!,self.downloadSize,self.totalSize!, self.ip)
        return logstr
    }
}
